package com.etc.thread;

import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Server {

	static List<MultiThread> clients = Collections.synchronizedList(new ArrayList<MultiThread>());
	
	public static void main(String[] args) {
		
		initFile();
		ServerSocket server = null;
		try {
			server = new ServerSocket(6700);
			System.out.println("服务器启动成功");
		} catch (Exception e) {
			System.out.println("服务器启动失败");
		}
		Socket socket = null;
		try {
			while(true) {
				socket = server.accept();
				MultiThread st = new MultiThread(socket);
				//创建线程并启动，为客户端提供服务
				Thread thread = new Thread(st);
				clients.add(st);
				thread.start();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void initFile() {
		File file = new File("D:/onLineRoles.dat");
		if(file.exists()) {
			file.delete();
		}
	}
	public void sendToAllClients(String message) {
		for(MultiThread con:clients){
			con.sendMessageToClient(message);
		}
	}
}
